package com.guigu.serviceorder.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.serviceorder.entity.TbOrder;
import com.guigu.serviceorder.entity.TbOrderItem;
import com.guigu.serviceorder.entity.TbSplitUtil;
import com.guigu.serviceorder.service.ITbOrderItemService;
import com.guigu.serviceorder.service.ITbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/order")
public class TbOrderController {

    @Autowired
    private ITbOrderService iTbOrderService;

    @Autowired
    private ITbOrderItemService iTbOrderItemService;

    // 分页条件查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody Map<String, Object> searchMap, Integer page, Integer size) {
        return iTbOrderService.findPage(searchMap, page, size);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody TbOrder tbOrder){
        if (iTbOrderService.save(tbOrder)==true)
            return Result.ok();
        return  Result.error();
    }

    @GetMapping("/delete")
    public Result delete(Long id){
        if(iTbOrderService.removeById(id))
            return Result.ok();
        return Result.error();

    }
    @PostMapping("/update")
    public Result update(@RequestBody TbOrder tbOrder){
        if (iTbOrderService.updateById(tbOrder))
            return Result.ok();
        return Result.error();
    }

    @GetMapping("/findById")
    public Result findById(Long id) {
        TbOrder tbOrder = iTbOrderService.getById(id);
        if (tbOrder == null) {
            return Result.error();
        }
        return Result.ok().data("tbOrder", tbOrder);
    }
    @PostMapping(value = "/batchSend",produces = "application/json")
    public Result batchSend(@RequestBody  Map map){
        System.out.println(map);
        String map1 =(String) map.get("map");
        List<TbOrder> orders = JSON.parseArray(map1, TbOrder.class);
        this.iTbOrderService.batchSend(orders);
        return Result.ok();
    }

    @PostMapping("/merge")
    public Result merge(@RequestBody Map map){
        String orderId1=map.get("orderId1").toString();
        String orderId2=map.get("orderId2").toString();
        this.iTbOrderService.merge(orderId1,orderId2);
//        if(this.iTbOrderService.merge(orderId1,orderId2)>0)
//            return Result.ok();
//        System.out.println("ssdsdsd");
        return Result.error();
    }
    @PostMapping(value = "/split",produces = "application/json")
    public Result split(@RequestBody Map map){

        //封装方法
        List<TbOrderItem> list1 = JSON.parseArray((String) map.get("map"), TbOrderItem.class);

        List<TbSplitUtil> list2 = JSON.parseArray((String) map.get("map"), TbSplitUtil.class);

        List<TbSplitUtil> tbSplitUtilList =new ArrayList<>();
        for (int i = 0;i<list1.size();i++){
            TbSplitUtil tbSplitUtil =new TbSplitUtil();
            tbSplitUtil.setSplitNum(list2.get(i).getSplitNum());
            tbSplitUtil.setTbOrderItem(list1.get(i));
            tbSplitUtilList.add(tbSplitUtil);
        }
        return this.iTbOrderService.split(tbSplitUtilList);
    }
    //@PostMapping(value = "/split",produces = "application/json")
//    @PostMapping(value = "/split")
//    public Result split(String map){
//       //封装方法
//        System.out.println(map);
//        List<TbOrderItem> list1 = JSON.parseArray((String) map.get("map"), TbOrderItem.class);
//
//        List<TbSplitUtil> list2 = JSON.parseArray((String) map.get("map"), TbSplitUtil.class);
//
//        List<TbSplitUtil> tbSplitUtilList =new ArrayList<>();
//        for (int i = 0;i<list1.size();i++){
//            TbSplitUtil tbSplitUtil =new TbSplitUtil();
//            tbSplitUtil.setSplitNum(list2.get(i).getSplitNum());
//            tbSplitUtil.setTbOrderItem(list1.get(i));
//            tbSplitUtilList.add(tbSplitUtil);
//        }
//        // 調用方法
//        this.iTbOrderService.split(tbSplitUtilList);
//
//        return null;
//    }
    @GetMapping("/findByOrderId")
    public Result findByOrderId(String OrderId){
        TbOrderItem tbOrderItem =this.iTbOrderService.findByOrderId(OrderId);
        if(tbOrderItem!=null){
            return Result.ok().data("tbOrderItem", tbOrderItem);
        }
        return Result.error();
    }
    @PostMapping(value = "/closeOrder",produces = "application/json")
    public Result closeOrder(@RequestBody  Map map){
        System.out.println("map"+map);
        String map1 =(String) map.get("map");
        System.out.println("map1"+map1);
//        System.out.println(JSON.parseArray(map1, TbOrder.class));
        List<TbOrder> orders = JSON.parseArray(map1, TbOrder.class);
        return this.iTbOrderService.closeOrder(orders);
    }
    @PostMapping(value = "/deleteOrder",produces = "application/json")
    public Result deleteOrder(@RequestBody  Map map){
        System.out.println(map);
        String map1 =(String) map.get("map");
        List<TbOrder> orders = JSON.parseArray(map1, TbOrder.class);
        return this.iTbOrderService.deleteOrder(orders);
    }
    @GetMapping("/selectOrderAndOrderItem")
    public Result selectOrderAndOrderItem(String orderId){
         Map map =this.iTbOrderService.selectOrderAndOrderItem(orderId);
        if(map != null)
            return Result.ok().data("map", map);
        return Result.error();
    }
    @GetMapping("/closeOrderOne")
    public Result closeOrderOne(String id){

        return this.iTbOrderService.closeOrderOne(id);
    }

    @GetMapping("/deleteOrderOne")
    public Result deleteOrderOne(String id){

        return this.iTbOrderService.deleteOrderOne(id);
    }
    @GetMapping("/confirmReceiptOne")
    public Result confirmReceiptOne(String id){
        return this.iTbOrderService.confirmReceiptOne(id);
    }
    @PostMapping(value = "/confirmReceipt",produces = "application/json")
    public Result confirmReceipt(@RequestBody  Map map){
        String map1 =(String) map.get("map");
        List<TbOrder> orders = JSON.parseArray(map1, TbOrder.class);
        return this.iTbOrderService.confirmReceipt(orders);
    }



    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("/saveOrder")
    public Map<String,Object> saveOrder(@RequestBody TbOrder order,String username){
        order.setUsername(username);
        return iTbOrderService.add(order);
    }

}

